package au.edu.unsw.editingtool.db;

import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Hashtable;

import au.edu.unsw.editingtool.global.EditingToolException;
import au.edu.unsw.editingtool.global.GlobalConstant;

public class DBPublication extends DBManager {
	
	private Hashtable<String,String> dbtable = null;
	
	protected void setupDBMapping() {
		if (MappingTable.size() > 0)
			return;
		MappingTable.put("Publication", "pid");
		MappingTable.put("ObjectStatus", "object_status");
		MappingTable.put("MetadataStatus", "metadata_status");
		MappingTable.put("Editor", "editor");
		MappingTable.put("LastReviewer", "last_reviewer");
		MappingTable.put("CurrentReviewer", "current_reviewer");
		MappingTable.put("FedoraVersion", "fedora_version");
		dbtable.put("Publication","");
		dbtable.put("ObjectStatus","");
		dbtable.put("MetadataStatus","");
		dbtable.put("Editor","");
		dbtable.put("LastReviewer","");
		dbtable.put("CurrentReviewer","");
		dbtable.put("FedoraVersion","");
	}	
	
	public DBPublication() {
		this.table = "publication";
		this.primaryKeys = new String[]{"Publication"};
		dbtable = new Hashtable<String,String>();
		this.setupDBMapping();		
	}
		
	public boolean getData(String pid) throws EditingToolException, SQLException, ClassNotFoundException 
	{
		dbtable.clear();
		boolean result =false;
		this.setTable(this.table, "");
		String fields[] = {"Publication","ObjectStatus","MetadataStatus","Editor","LastReviewer","CurrentReviewer","FedoraVersion"};
		this.setFieldsCondition("Publication", this.setEqual(pid));
		this.addField(fields);
		
		this.initDB();
		this.selectRecords();
		
		if (this.ResultSet == null) {
			System.out.println("[DBPUBLICATION] getData Resultset = NULL");
			this.clean();
			return result;
		}
		int count =0;
		while(ResultSet.next())
		{
			count++;
			if(ResultSet.getObject(1)!=null)
				dbtable.put("Publication",ResultSet.getObject(1).toString());
			if(ResultSet.getObject(2)!=null)
				dbtable.put("ObjectStatus", ResultSet.getObject(2).toString());
			if(ResultSet.getObject(3)!=null)
				dbtable.put("MetadataStatus",ResultSet.getObject(3).toString());
			if(ResultSet.getObject(4)!=null)
				dbtable.put("Editor",ResultSet.getObject(4).toString());
			if(ResultSet.getObject(5)!=null)
				dbtable.put("LastReviewer",ResultSet.getObject(5).toString());
			if(ResultSet.getObject(6)!=null)
				dbtable.put("CurrentReviewer",ResultSet.getObject(6).toString());
			if(ResultSet.getObject(7)!=null)
				dbtable.put("FedoraVersion",ResultSet.getObject(7).toString());
		}
		Enumeration keys = dbtable.keys();
		while(keys.hasMoreElements())
		{
			String key = (String) keys.nextElement();
			if(dbtable.get(key)==null)
				dbtable.put(key,"");
		}
		if(count==1)
		{				
			result = true;
			System.out.println("[DBPUBLICATION] getData Resultset = 1");			
		}
		else
			dbtable.clear();
		
		this.clean();
		return result;		
	}
	
	public void commit() throws EditingToolException, SQLException, ClassNotFoundException {
		this.setTable(this.table, "");
		this.setColumnValues(dbtable);
		this.initDB();
		this.insert(false, true);
		this.clean();
		this.dbtable.clear();
	}
	
	public String getPublication(){return dbtable.get("Publication");}	
	public String getObjectStatus(){return dbtable.get("ObjectStatus");}
	public String getMetadataStatus(){return dbtable.get("MetadataStatus");}
	public String getEditor(){return dbtable.get("Editor");}
	public String getLastReviewer(){return dbtable.get("LastReviewer");}
	public String getCurrentReviewer(){return dbtable.get("CurrentReviewer");}
	public String getFedoraVersion(){return dbtable.get("FedoraVersion");}

	public void setPublication(String somevalue){
		if(somevalue==null)
			dbtable.put("Publication","");
		else
			dbtable.put("Publication",somevalue);
	}	
	public void setObjectStatus(String somevalue){
		if(somevalue==null)
			dbtable.put("ObjectStatus","");
		else
			dbtable.put("ObjectStatus",somevalue);
	}
	public void setMetadataStatus(String somevalue){
		if(somevalue==null)
			dbtable.put("MetadataStatus","");
		else
			dbtable.put("MetadataStatus",somevalue);
	}
	public void setEditor(String somevalue){
		if(somevalue==null)
			dbtable.put("Editor","");
		else
			dbtable.put("Editor",somevalue);
	}
	public void setLastReviewer(String somevalue){
		if(somevalue==null)
			dbtable.put("LastReviewer","");
		else
			dbtable.put("LastReviewer",somevalue);
	}
	public void setCurrentReviewer(String somevalue){
		if(somevalue==null)
			dbtable.put("CurrentReviewer","");
		else
			dbtable.put("CurrentReviewer",somevalue);
	}
	public void setFedoraVersion(String somevalue){
		if(somevalue==null)
			dbtable.put("FedoraVersion","");
		else
			dbtable.put("FedoraVersion",somevalue);
	}
}
